import {all, create} from 'mathjs';

/**
 * formTitle
 * @type {{text: string, value: string}[]}
 */
export const formTitle = {
  'add': '配件采购提报',
  'edit': '配件采购编辑',
  'show': '配件采购明细',
}
/**
 * 订单状态常量
 * @type {{text: string, value: string}[]}
 */


/**
 * 订单明细状态常量
 * @type {{text: string, value: string}[]}
 */
export const mxddztArr = [
  {text: '已保存', value: 'C'},
  {text: '已提报', value: 'T'},
  {text: '取消申请', value: 'Q'}, //（已提报的取消）
  {text: '总部审批通过', value: 'S'},
  {text: '总部审批驳回', value: 'B'}, //（特殊追溯CMS总部驳回）
  {text: '已评审', value: 'P'},
  {text: '已发运', value: 'F'},
  {text: '经销商BO取消申请', value: 'BO-DLR-Q'},
  {text: 'SAP-BO驳回', value: 'BO-SAP-B'},
  {text: 'SAP-BO取消申请', value: 'BO-SAP-Q'},
  {text: '经销商BO驳回', value: 'BO-DLR-B'},
  {text: '已取消', value: 'Q2'}, //(经销商和SAP)
  {text: '已开票', value: 'K'},
  {text: '已签收', value: 'QS'}
]

/**
 * 订单类型常量
 * @type {{text: string, value: string}[]}
 */
const ddlx = [
  {text: '常规订单', value: 'S', appId: ['beijing', 'arcfox']},
  {text: '紧急订单', value: 'E', appId: ['beijing', 'arcfox']},
  {text: '精品订单', value: 'J', appId: ['beijing']},
  {text: '专项订单', value: 'Z', appId: ['beijing', 'arcfox']},
  {text: '促销订单', value: 'P', appId: ['beijing']},
  {text: '直供订单', value: 'T', appId: ['beijing', 'arcfox']},
  {text: '三包订单', value: 'B', appId: ['beijing', 'arcfox']},
  {text: '特殊追溯订单', value: 'Q', appId: ['beijing', 'arcfox']},
  {text: '免费出库订单', value: 'M', appId: ['beijing']},
  {text: '油辅料订单', value: 'O', appId: ['beijing']},
  {text: '首批订单', value: 'I', appId: ['beijing']},
  {text: '其他订单', value: 'A', appId: ['beijing']},
]
const appId = localStorage.getItem('appId');
export const ddlxArr = ddlx.filter(i => i.appId.includes(appId));


/**
 * 订单提报类型常量
 * @type {{text: string, value: string}[]}
 */
export const ddtblxArr = [
  {text: '经销商提报', value: '1'},
  {text: '总部代提报', value: '2'},
]

/**
 * 订单提报类型常量
 * @type {{text: string, value: string}[]}
 */
export const ddtshArr = [
  {text: '商务审核通过', value: '1'},
  {text: '商务审核驳回', value: '2'},
  {text: '财务审核通过', value: '3'},
  {text: '财务审核驳回', value: '4'},
]

/**
 * 计算配件明细row价格信息
 */
export function calcDetails(detail) {
  const mathjs = create(all);
  mathjs.config({
    number: 'BigNumber',      // Default type of number:
    precision: 64             // Number of significant digits for BigNumbers
  })
  let {round, subtract, chain, add} = mathjs
  // 单价
  let culdj = chain(detail.xsxsdj);
  //总金额
  let culze = culdj.multiply(detail.bstxsl);
  // 加收的单价
  let js = chain(0);
  // 折扣掉的单价
  let  zk = chain(0);
  detail.discounts2.forEach(i => {
    //折扣总金额
    let zkje
    //百分比
    if (i.xsgzlx == '2') {
      // culdj = culdj * ((100 - Number(i.xtjgjg)) / 100);
      if (i.xtlxdm == 'ZK90') {
        js = js.add(culdj.multiply(subtract(0, i.xtjgjg)).divide(100).value)
        culdj = culdj.multiply(subtract(100, i.xtjgjg)).divide(100);
      } else {
        zk = zk.add(culdj.multiply(subtract(0, i.xtjgjg)).divide(100).value)
        culdj = culdj.multiply(subtract(100, i.xtjgjg)).divide(100);
      }
      zkje = culze.multiply(subtract(0, i.xtjgjg)).round(2).divide(100).round(2);
    } else { // 金额
      // culdj = culdj - Number(i.xtjgjg)
      if (i.xtlxdm == 'ZK90') {
        js = js.subtract(i.xtjgjg)
        culdj = culdj.subtract(i.xtjgjg);
      } else {
        zk = zk.subtract(i.xtjgjg)
        culdj = culdj.subtract(i.xtjgjg);
      }
      zkje = chain(subtract(0, i.xtjgjg)).multiply(detail.bstxsl).round(2)
    }
    culze = culze.add(zkje.value);
  });
  // 折扣单价
  // detail.pjzkl = (detail.xsxsdj - culdj).toFixed(2);
  // detail.pjzkl = culdj.subtract(zkhdj.value).round(2).toString();
  detail.pjzkl = zk.round(2).toString();
  // 加收单价
  // detail.jsglfl = (culdj - zkhdj).toFixed(2);
  detail.jsglfl = js.round(2).toString();

  // // 加价单价
  // const zkhdj = `${culdj}`;
  // detail.discounts2.forEach(i => {
  //   //折扣总金额
  //   let zkje
  //   //百分比
  //   if (i.xsgzlx == '2') {
  //     // culdj = culdj * ((100 - Number(i.xtjgjg)) / 100);
  //     culdj = culdj.multiply(subtract(100, i.xtjgjg)).divide(100);
  //     zkje = culze.multiply(i.xtjgjg).divide(100).round(2);
  //   } else {
  //     // culdj = culdj - Number(i.xtjgjg);
  //     culdj = culdj.subtract(i.xtjgjg);
  //     zkje = chain(i.xtjgjg).multiply(detail.bstxsl).round(2)
  //   }
  //   culze = culze.subtract(zkje.value);
  // });

  // 加价后单价（折扣价格）
  // detail.jjhdj = culdj.toFixed(2);
  detail.jjhdj = culdj.round(2).toString();
  // 加价后总额
  detail.xsysje = culze.value
}

/**
 * 构造明细行数据
 * @data
 * @isJsY 油配件是否加收管理费 （Y,N）
 * @isJsD 电配件是否加收管理费 （Y,N）
 **/
export function getPartDetails(data, isJsY, isJsD) {
  const details = data.details.map(i => {
    const centralWarehouseInfo = (i.partInfo.bjpjsx == 50001001 ? data.centralWarehouseInfoY : data.centralWarehouseInfoD);
    const obj = {
      partInfoId: i.partInfo.id,
      partInfo: {
        bjxmbh: i.partInfo.bjxmbh,
        bjzwmc: i.partInfo.bjzwmc,
        bjpjsx: i.partInfo.bjpjsx,
        bjpjfl: i.partInfo.bjpjfl,
        jsglfl: i.partInfo.jsglfl
      },
      centralWarehouseId: centralWarehouseInfo.id,
      centralWarehouse: {...centralWarehouseInfo},
      bjxszx: i.partInfo.bjcgzx,
      xsxsdj: i.partInfo.xs,
      pjzklx: i.partInfo.xtqzlx || 0,
      pjzkl: i.partInfo.xtjgjg || 0,
      jsglfl: 0,
      jjhdj: 0,
      cgjhsl: 0,
      bstxsl: i.bstxsl,
      bjdanw: i.partInfo.bjdanw,
      xsysje: 0
    }
    const discounts = i.partInfo.discounts.map(d => {
      return {
        partInfoId: d.partInfoId,
        xsgzlx: d.xtqzlx,
        xtjgjg: d.xtjgjg,
        xtlxdm: d.xtlxdm,
        discountStrategyDetailId: d.id,
        orderNo: d.orderNo
      }
    });
    // obj.discounts = discounts.filter(d => d.xtlxdm != 'ZK90');
    obj.discounts2 = discounts.filter(d => {
      if (d.xtlxdm != 'ZK90') {
        return true;
      } else {
        //指定配件的加价率不用考虑次数
        if (d.partInfoId) return true;

        const partOrderTypeInfo = data.partOrderTypeInfo;

        let ytbcs; //油电提报次数分开
        if (i.partInfo.bjpjsx == 50001001) { //油
          if (isJsY) return isJsY == 'Y';
          ytbcs = partOrderTypeInfo.ycs;
        } else { //电
          if (isJsD) return isJsD == 'Y';
          ytbcs = partOrderTypeInfo.dcs;
        }

        if (!partOrderTypeInfo) return false;

        // 如果大于设置的月提报次数 需要加收管理费率
        return ytbcs >= partOrderTypeInfo.pjlxcs;
      }
    });
    return obj
  })
  details.forEach(calcDetails);
  return details;
}

//计算订单总提报金额，总项目行数，总配件采购数量
export function culOrderData(partOrder) {
  partOrder.details.forEach((i, index) => i.dmsdhh = index + 1);
  partOrder.details.forEach(calcDetails);
  const amontObj = partOrder.details.reduce((obj, i) => {
    return {
      bszsls: obj.bszsls + Number(i.bstxsl),
      xsysje: obj.xsysje + Number(i.xsysje)
    };
  }, {bszsls: 0, xsysje: 0});
  partOrder.bszsls = amontObj.bszsls;
  partOrder.xsysje = amontObj.xsysje;
  partOrder.bszpzs = partOrder.details.length;
  const obj = {...partOrder};
  if (partOrder.partOrderTypeInfo) {
    if (!obj.isJsY) {
      const hasY = obj.details.some(i => i.partInfo.bjpjsx == 50001001);
      if (hasY) {
        obj.isJsY = partOrder.partOrderTypeInfo.ycs >= partOrder.partOrderTypeInfo.pjlxcs ? 'Y' : 'N';
      }
    }
    if (!partOrder.isJsD) {
      const hasD = obj.details.some(i => i.partInfo.bjpjsx == 50001002);
      if (hasD) {
        obj.isJsD = partOrder.partOrderTypeInfo.dcs >= partOrder.partOrderTypeInfo.pjlxcs ? 'Y' : 'N';
      }
    }
  }
  return obj;
}
